// pages/mainOperation/myAddressList/myAddressList.js
//获取应用实例
const app = getApp()
let utils = require('../../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    PageIndex: 1, //页码
    PageSize: 10, //页大小
    PageCount: 0, //总数量
    SourceUrl: '', //页面来源
    addList: [],
    currentAddressId: 0, //当前被选中的地址
    noData: true, //暂无数据
    noDataText: '暂无数据', //暂无数据的文本
    noDataHeight: '500', //暂无数据高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData(options);
  },
  /**
   *  选择地址
   */
  choiceAddressTap: function(e) {
    let self = this;
    let aid = e.currentTarget.dataset.aid;
    if (self.data.SourceUrl == 'Order') {
      let username = e.currentTarget.dataset.username;
      let usermobile = e.currentTarget.dataset.usermobile;
      let areaaddress = e.currentTarget.dataset.areaaddress;
      let address = e.currentTarget.dataset.address;
      //获取上一页
      let pages = getCurrentPages();
      let prePage = pages[pages.length - 2];
      prePage.setData({
        HasDftAddress: true, //是否存在默认地址
        MailUserName: username, //收件名称
        MailUserMobile: usermobile, //收件电话
        MailAreaAddress: areaaddress, //收件省市
        MailAddress: address, //收件地址
        MailAddressID: aid //收件主键
      });

      wx.navigateBack({
        delta: 1 // 返回上一级页面。
      });
    }

  },
  /**
   * 查询消息列表
   */
  getAddressList: function() {
    let self = this;
    if (app.userInfo.UserID <= 0) {
      self.setData({
        addList: [],
        noData: true
      });
      return;
    }
    let url = app.apiBaseUrl + "/MailAddress/GetList";

    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: true,
      data: {
        UserID: app.userInfo.UserID,
        PageIndex: self.data.PageIndex,
        PageSize: self.data.PageSize
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          if (self.data.PageIndex == 1) {
            //根据数据动态显示是否暂无数据
            if (rdata.data.length > 0) {
              //计算总页数
              let recordCount = parseInt(rdata.message);
              if (recordCount > 0) {
                self.data.PageCount = utils.evalPageCount(recordCount, self.data.PageSize);
              }
              self.setData({
                addList: rdata.data,
                noData: false
              });
            } else {
              self.setData({
                addList: [],
                noData: true
              });
            }
          } else {
            self.data.addList.push(...rdata.data);
            self.setData({
              addList: self.data.addList
            });
          }
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  /**
   *  编辑地址
   */
  goToEditAddressTap: function(e) {
    let aid = e.currentTarget.dataset.aid;
    wx.navigateTo({
      url: '/pages/my/myAddress/editAddress/editAddress?MailAddressID=' + aid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let self = this;
    self.getAddressList();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})